package elicecn.data.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONObject;

public class Weather {
	// 数据结构
	// 城市cityName
	// .|-当天/第二天/第三天resultList
	// ....|-白天/晚间weatherContent
	// ........|-时间dayNight：白天、晚间
	// ........|-天气weather
	// ........|-天气图片weatherImage
	// ........|-温度temperature
	public static final String CITY_NAME = "cityName";
	public static final String RESULT_LIST = "resultList";
	public static final String WEATHER_CONTENT = "weatherContent";
	public static final String DAY_NIGHT = "dayNight";
	public static final String WEATHER = "weather";
	public static final String WEATHER_IMAGE = "weatherImage";
	public static final String TEMPERATURE = "temperature";
	//
	public static final String DAY = "白天";
	public static final String NIGHT = "夜间";
	// 获取天气用到的网址
	private static final String BAIDU_URL = "http://www.baidu.com/s?wd=";
	private static final String WEATHER_URL = "http://www.weather.com.cn/weather/";
	private static final String WEATHER_LOC = "http://www.weather.com.cn";

	/**
	 * 根据城市名获取中国天气网URL地址
	 * 
	 * @param cityName
	 * @return
	 */
	private static String weatherUrl(String cityName) {
		String weatherUrl = null;
		if (cityName != null && !cityName.equals("")) {
			String searchUrl = BAIDU_URL + cityName + "天气";
			try {
				String html = Request.httpGet(searchUrl);
				int idx = html.indexOf(WEATHER_URL);
				html = html.substring(idx);
				idx = html.indexOf("\"");
				html = html.substring(0, idx);
				weatherUrl = html;
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return weatherUrl;
	}

	/**
	 * 根据城市名获取天气信息
	 * 
	 * @param url
	 * @return
	 */
	public static Map<String, Object> weather(String cityName) {
		Map<String, Object> resultMap = new HashMap<String, Object>(0);
		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>(
				0);
		resultMap.put(CITY_NAME, cityName);
		resultMap.put(RESULT_LIST, resultList);
		try {
			String url = weatherUrl(cityName);
			String html = Request.httpGet(url);
			String[] part = html.split("yuBaoTable");
			if (part.length == 8) {
				for (int i = 1; i < 8; i++) {// 第一天到第七天天气
					Map<String, Object> map = parseDateWeather(part[i]);
					resultList.add(map);
				}
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return resultMap;
	}

	/**
	 * 分析单天天气信息
	 * 
	 * @param html
	 * @return
	 */
	private static Map<String, Object> parseDateWeather(String html) {
		Map<String, Object> resultMap = new HashMap<String, Object>(0);
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(0);
		if (html.indexOf(DAY) != -1) {
			String ctt = html.split(DAY)[1];
			Map<String, Object> map = parseDayNightWeather(ctt, DAY);
			list.add(map);
		}
		if (html.indexOf(NIGHT) != -1) {
			String ctt = html.split(NIGHT)[1];
			Map<String, Object> map = parseDayNightWeather(ctt, NIGHT);
			list.add(map);
		}
		resultMap.put(WEATHER_CONTENT, list);
		return resultMap;
	}

	/**
	 * 分析白天或晚间的天气
	 * 
	 * @param html
	 * @return
	 */
	private static Map<String, Object> parseDayNightWeather(String html,
			String dayNight) {
		Map<String, Object> weatherContent = new HashMap<String, Object>(0);
		String weather = "", image = "", temp = "";
		weather = parseWeatherContent(html);
		image = parseWeatherImage(html);
		temp = parseWeatherTemperature(html);
		weatherContent.put(DAY_NIGHT, dayNight);// 时间
		weatherContent.put(WEATHER, weather);// 天气
		weatherContent.put(WEATHER_IMAGE, image);// 图片
		weatherContent.put(TEMPERATURE, temp);// 温度
		return weatherContent;
	}

	// 天气
	private static String parseWeatherContent(final String html) {
		String htm = html;
		String prefix = "<img src=\"";
		String suffix = "</a>";
		String suffix2 = ">";
		int idx = htm.indexOf(prefix);
		htm = htm.substring(idx);
		idx = htm.indexOf(suffix);
		idx = htm.indexOf(suffix, idx + suffix.length());
		htm = htm.substring(0, idx);
		idx = htm.lastIndexOf(suffix2);
		htm = htm.substring(idx + suffix2.length());
		return htm;
	}

	// 天气图片
	private static String parseWeatherImage(final String html) {
		String htm = html;
		String prifix = "<img src=\"";
		String suffix = "\"";
		int idx = htm.indexOf(prifix);
		htm = htm.substring(idx + prifix.length());
		idx = htm.indexOf(suffix);
		htm = htm.substring(0, idx);
		return WEATHER_LOC + htm;
	}

	// 气温
	private static String parseWeatherTemperature(final String html) {
		String htm = html;
		String keyword = "<strong>";
		String suffix = "<";
		int idx = htm.indexOf(keyword);
		htm = htm.substring(idx + keyword.length());
		idx = htm.indexOf(suffix);
		htm = htm.substring(0, idx);
		return htm;
	}

	public static void main(String[] args) {
		System.out.println(new JSONObject(weather("锡林浩特")));
	}
}
